// pages/voteinfo/voteinfo.js
const app = getApp()
var request = require('../../utils/request.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    voteId:0,
    qnaire: [],
    answer: [],
    username:"test"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      username: app.globalData.userInfo.nickName
    });
    wx.setNavigationBarTitle({
      title: '问卷调查'
    })
    console.log(options.vId);
    this.setData({
      voteId: options.vId
    });
    //获取试卷
    this.getVoteInfos();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  radioChange: function (e) {
    //题目的id
    let sid = e.currentTarget.dataset.sid;
    //选项的id
    let id = e.currentTarget.dataset.id;
    let value = e.detail.value;
    console.log(e.detail.value);
    let aw = this.data.answer;
    //添加用户答案
    if (aw){
      //循环答案。判断是不是已经回答过的更改
      let tag = 0;
      for (let i = 0; i < aw.length;i++){
        if (aw[i].id==id){
          aw[i]["answer"] = value;
          tag=1;
          break
        }
      }
      if(tag==0){
        aw.push(
          {
            "id": id,
            "username": this.data.username,
            "answer": value,
            "vote_info_id": sid
          }
        );
      }
    }else{
      aw.push(
        {
          "id":id,
          "username":this.data.username,
          "answer": value,
          "vote_info_id": sid
        }
      );
    }
    this.setData({
      answer: aw
    })
    console.log(aw);
  },

  submit: function (e) {
    console.log(this.data.answer);
    //提交用户回答
    let that = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: 'http://www.wx.com/?type=add_vote_answer',
      method: "post",
      data:{
        data:JSON.stringify(that.data.answer)
      },
      success: function (res) {
        wx.showToast({
          title: '投票成功',
          success:function(){
            //跳转到列表
            wx.switchTab({
              url: '../home/home'
            })
          }
        })
      },
      fail: function (error) {
        wx.showToast({
          title: '请求失败',
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    })
  },
  getVoteInfos:function(){
    let that = this;
    request.getData(
        '?type=vote_ifo&vote_id='+this.data.voteId,
        (d)=>{
          let data = d.data;
          //解析数据
          for (let i = 0; i < data.length; i++) {
            let options = data[i]["answer_option"];
            data[i]["answer_option"] = JSON.parse(options);
          }
          console.log(data);
          if (data) {
            that.setData({
              qnaire: data
            });
          }
        },
        (err)=>{

        }
    );
  },
  //监听填空选择
  inputTxt:function(e){
    console.log("填空选择的值：" + e.detail.value);
    let value = e.detail.value;
    //题目的id
    let sid = e.currentTarget.dataset.sid;
    //选项的id
    let id = e.currentTarget.dataset.id;

    let aw = this.data.answer;
    //添加用户答案
    if (aw) {
      //循环答案。判断是不是已经回答过的更改
      let tag = 0;
      for (let i = 0; i < aw.length; i++) {
        if (aw[i].id == id) {
          aw[i]["answer"] = value;
          tag = 1;
          break
        }
      }
      if (tag == 0) {
        aw.push(
          {
            "id": id,
            "username": this.data.username,
            "answer": value,
            "vote_info_id": sid
          }
        );
      }
    } else {
      aw.push(
        {
          "id": id,
          "username": this.data.username,
          "answer": value,
          "vote_info_id": sid
        }
      );
    }
    this.setData({
      answer: aw
    })
    console.log(aw);
  },
  checkboxChanage:function(e){
    let value = e.detail.value;
    //题目的id
    let sid = e.currentTarget.dataset.sid;
    //选项的id
    let id = e.currentTarget.dataset.id;
    console.log(sid);
    console.log(id);
    console.log(value);


    let aw = this.data.answer;
    //添加用户答案
    if (aw) {
      //循环答案。判断是不是已经回答过的更改
      let tag = 0;
      for (let i = 0; i < aw.length; i++) {
        if (aw[i].id == id) {
          aw[i]["answer"] = id;
          tag = 1;
          break
        }
      }
      if (tag == 0) {
        aw.push(
          {
            "id": id,
            "username": this.data.username,
            "answer": id,
            "vote_info_id": sid
          }
        );
      }
    } else {
      aw.push(
        {
          "id": id,
          "username": this.data.username,
          "answer": id,
          "vote_info_id": sid
        }
      );
    }
    this.setData({
      answer: aw
    })
    console.log(aw);
  }
})